package com.example.chinalin.viewpagerdemo;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;

import com.example.chinalin.viewpagerdemo.entities.People;
import com.raizlabs.android.dbflow.runtime.transaction.process.ProcessModelInfo;
import com.raizlabs.android.dbflow.runtime.transaction.process.SaveModelTransaction;
import com.raizlabs.android.dbflow.sql.language.Select;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static org.junit.Assert.assertEquals;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.example.chinalin.viewpagerdemo", appContext.getPackageName());
    }

    @Test
    public void dbFlowTest(){
/*        SQLCondition condition = People_Table.id.greaterThan(0L);
        Delete.table(People.class, condition);
        People people = new People();
        people.name = "Wiki";
        people.gender = 1;
        people.save();
        People people1 = new People();
        people1.name = "lin";
        people1.gender = 0;
        people1.save();*/

        ArrayList<People> list=new ArrayList<>();
        for (int i=0;i<10;i++)
        {
            People people = new People();
            people.name = "张三" + i;
            people.gender = 1;
            list.add(people);
        }
        ///实时保存，马上保存
        new SaveModelTransaction<>(ProcessModelInfo.withModels(list)).onExecute();
        //异步保存，使用异步，如果立刻查询可能无法查到结果
        //TransactionManager.getInstance().addTransaction(new SaveModelTransaction<>(ProcessModelInfo.withModels(peoples)));
        List<People> peoples = new Select().from(People.class).queryList();
        Iterator ps = peoples.iterator();
        People people = new People();
        while (ps.hasNext()){
            people = (People) ps.next();
            Log.e("dbflow: " , people.id + "   " + people.name + "   " + people.gender);
        }
        //people.update();
        //people.delete();
    }
}
